/**
 * Created by kangdaye on 16/6/5.
 */
app.controller('freightAECtr',["$scope","$rootScope","$stateParams","ngDialog","confirm","freightCache","freightService", function($scope,$rootScope,$stateParams,ngDialog,confirm,freightCache,freightService) {
    $scope.getData = {
        inclPostage : 'true',
        freightRulesList : [],
        valuationModel : 2
    };
    $scope.gridOptions = {
        columnDefs   : freightCache.detailTableWeightHeader,
        data : [{
            defaul : false
        }],
        showPage : false
    };

    $scope.viewData = {
        detailPostage : freightCache.detailPostage,
        detailPricing : freightCache.detailPricing,
        companyList   : {},
        defaul        : {
            defaul : true
        }
    };

    function dataJsonString(data){
        var copyData = angular.copy(data);
        copyData.freightRulesList = angular.copy($scope.gridOptions.data);
        copyData.freightRulesList.push($scope.viewData.defaul || {});
        copyData.freightRules = JSON.stringify(copyData.freightRulesList);
        delete copyData.freightRulesList;
        return copyData;
    }

    $scope.submit = function(){
        var getData;
        if($scope.getData.inclPostage === 'true' || !$scope.getData.inclPostage){
            getData = $scope.getData;
        }else{
            getData = dataJsonString($scope.getData);
        }
        if(!$stateParams.id){
            freightService.add(getData,function(){
                location.href = $rootScope.prefix + 'goods/freight/list.html';
            });
            return;
        }
        freightService.update(getData,function(){
            location.href = $rootScope.prefix + 'goods/freight/list.html';
        });
    };

    $scope.selectArea = function(row){
        ngDialog.open({
            template: 'view/common/selectAreaAll.html',
            controller: 'selectAreaAllCtr',
            width : 750,
            resolve: {
                selectDatas : function(){
                    return {
                        selectData : row.region,
                        selectCallback : function(selectName,selectData,next){
                            row.region = selectData;
                            next();   //next是提供出一个类似co模块断点功能,后期可能校验;
                        }
                    }
                }
            }
        });
    };

    $scope.delete = function(index){
        confirm({
            text:'确定要删除吗？',
            option:{
                go:function(){
                    $scope.gridOptions.data.splice(index,1);
                    message({text : '删除成功'});
                }
            }
        });
    };

    $scope.add = function(index){
        $scope.gridOptions.data.push({
            defaul : false
        });
    };

    $scope.pricingToggle = function(valuationModel){
        if(valuationModel == 1){
            $scope.gridOptions.columnDefs = freightCache.detailTablePieceHeader;
            return;
        }
        $scope.gridOptions.columnDefs = freightCache.detailTableWeightHeader;
    };

    if($stateParams.id){
        freightService.getDetail($stateParams,function(data){
            $scope.getData = data.data;
            $scope.getData.inclPostage = $scope.getData.inclPostage.toString();
            $scope.gridOptions.data = data.data.freightRulesList;
            
            $scope.gridOptions.data.forEach(function(item,index){
                if(item.defaul){
                    $scope.gridOptions.data.splice(index,1);
                    $scope.viewData.defaul = item;
                }
            });
        });
    }
}]);
